package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisMessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.Order;
import com.itheima.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import java.util.Map;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Reference
    private OrderService orderService;
    @Autowired
    private JedisPool jedisPool;

    //提交预约信息
    @RequestMapping("/submit")
    public Result submit(@RequestBody Map map) throws Exception {
        //获取手机号
        String telephone = (String) map.get("telephone");
        //取出redis中的验证码
        String c_code = jedisPool.getResource().get(telephone + RedisMessageConstant.SENDTYPE_ORDER);
        //获取用户输入的验证码
        String v_code = (String) map.get("validateCode");
        //校验手机验证码
        System.out.println(c_code + "==" + v_code);
        if (c_code == null || v_code == null || !c_code.equals(v_code)) {
            return new Result(false, MessageConstant.VALIDATECODE_ERROR);
        }
        map.put("orderType", Order.ORDERTYPE_WEIXIN);
        Result submit = orderService.submit(map);
        //发送短信
        // SMSUtils.sendShortMessage(SMSUtils.ORDER_NOTICE,telephone,validateCode);
        System.out.println(v_code);
        //返回结果
        return submit;
    }
    //查询预约信息
    @RequestMapping("/findById")
    public Result findById(Integer id){
        Result result = orderService.findById(id);
        return result;
    }

}
